// 分页混入
export default {
  data() {
    return {
      loadingStatus: 'loadmore', // loadmore--加载更多 loading--加载中, nomore--没有更多了
      empty: true, // 标记是否没数据
      pagination: {
        pageNo: 1,
        pageSize: 10,
      },
      total: 0,// 总数
      list: [], // 列表数据
      delayGet: false, // 是否需要延迟加载，常用与路由传参，需要拼接query参数
      noInit: false, // 是否不进行初始化加载，常用于初始化手动调用
      keepOnload: false
    }
  },

  async onLoad() {
    this.onload = true;
    if (this.delayGet) {
      await this.$nextTick()
    }
    // 外部初始化 不加载
    if (this.noInit) {
      this.onload = false;
      return
    }
    await this.getList();
    this.onload = false
  },

  onShow() {
    if (this.onload) return
    if (this.keepOnload) return
    console.log(1);
    this.getList(true)
  },

  // 触底触发
  onReachBottom() {
    if (['nomore', 'loading'].includes(this.loadingStatus)) return
    this.pagination.pageNo += 1
    this.getList(); // 加载数据
  },

  methods: {
    /**
     * 公共的获取数据方法 支持重写
     * @param {是否重新获取} refresh
     */
    async getList(refresh) {
      if (refresh) {
        this.pagination.pageNo = 1;
        this.list = []
      }
      if (!this.queryApi) {
        throw new Error('列表查询方法不能为空')
      }
      try {
        this.loadingStatus = 'loading'
        const response = await this.queryApi({
          ...(this.queryParams || {}),
          ...this.pagination
        })
        if (this.customerPageRender) {
          this.customerPageRender(response);
          return
        }
        this.domainPageFunc(response);
      } catch (error) {

      }

    },
    // 初始化方法
    initData() {
      this.list = []
      this.pagination = {
        pageNo: 1,
        pageSize: 10,
      }
      this.loadingStatus = 'loadmore',
        this.total = 0
    },

    // 分页操作
    domainPageFunc(response) {
      const { total, list = [] } = response;
      this.list = [].concat(this.list, list || []);
      this.total = total;
      this.loadingStatus = this.list.length >= total ? 'nomore' : 'loadmore';
      this.empty = this.list.length === 0; // 是否是没数据
    }
  }


}
